from flask_restful import Resource, reqparse
from app.errcode import errcode as errcode
from flask import abort

class BaseResource(Resource):
	def __init__(self):
		self.code = errcode
		self.parser = reqparse.RequestParser()
		self.condition = {}
		super(BaseResource, self).__init__()

	def add_arguments(self, args=[]):
		for item in args:
			self.parser.add_argument(**item)

	def get_args(self):
		RFKL = RFVL = [None, {}, "", []]
		args = self.parser.parse_args(strict=True)
		args_filter = dict(filter(lambda x: not (
				x[0] in RFKL or x[1] in RFVL), args.items()))
		for item in args_filter:
			if item in self.condition.keys():
				a, b = self.condition[item](args_filter[item])
				if a is False:
					abort(400, {"errcode": b, "errmsg": errcode[b]})
		return args_filter
